﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using SharepointListFieldsMassiveCreator.SPSReference;

namespace SharepointListFieldsMassiveCreator
{
    public partial class MainForm : Form
    {
        private List<FieldsDTO> fieldsList;

        public MainForm()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult r = this.ofdSelectListDefinitionFile.ShowDialog();
            if (r == DialogResult.OK || r == DialogResult.Yes)
            {
                this.txtSourceFile.Text = ofdSelectListDefinitionFile.FileName;
            }
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            fieldsList = null;
            fieldsList = new List<FieldsDTO>();

            String[] fields = System.IO.File.ReadAllLines(this.txtSourceFile.Text);
            foreach (string field in fields)
            {
                if (field.Contains(','))
                {
                    if (field.Contains('|'))
                        this.fieldsList.Add(this.BuildFieldDTOItemByStringWithCommaAndPipe(field));
                    else
                        this.fieldsList.Add(this.BuildFieldDTOItemByStringWithComma(field));
                }
                else
                {
                    this.fieldsList.Add(this.BuildFieldDTOItem(field, "Text"));
                }
            }
            this.dgvIdentifiedFieldsList.DataSource = fieldsList;
        }

        private FieldsDTO BuildFieldDTOItemByStringWithCommaAndPipe(string field)
        {

            return new FieldsDTO() { FieldName = field.Split(',')[0], FieldType = field.Split(',')[1].Split('|')[0], FieldDefinition = field.Split(',')[1].Split('|')[1] };
        }

        private FieldsDTO BuildFieldDTOItemByStringWithComma(String field)
        {
            return new FieldsDTO() { FieldName = field.Split(',')[0], FieldType = field.Split(',')[1], FieldDefinition = "<Field><Type>" + field.Split(',')[1] + "</Type></Field>" };
        }

        private FieldsDTO BuildFieldDTOItem(String fieldName, String fieldType)
        {
            return new FieldsDTO() { FieldName = fieldName, FieldType = fieldType, FieldDefinition = "<Field><Type MaxLength=\"50\">Text</Type></Field>" };
        }

        public class FieldsDTO
        {
            public String FieldName { get; set; }
            public String FieldType { get; set; }
            public String FieldDefinition { get; set; }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnGenerateGUID_Click(object sender, EventArgs e)
        {
            this.txtGUID.Text = System.Guid.NewGuid().ToString().Replace("{", "").Replace("}", "");
        }

        private void btnBuildList_Click(object sender, EventArgs e)
        {
            SPSReference.Lists service;
            using (service = new Lists())
            {
                service.Url = System.Configuration.ConfigurationManager.AppSettings["serviceUrl"];
                service.UseDefaultCredentials = false;
                service.Timeout = System.Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["serviceTimeout"]);
                NetworkCredential credential = new NetworkCredential(
                    System.Configuration.ConfigurationManager.AppSettings["serviceUserName"],
                    System.Configuration.ConfigurationManager.AppSettings["servicePassword"],
                    System.Configuration.ConfigurationManager.AppSettings["serviceDomain"]);
                service.Credentials = credential;
                var nodo = service.AddList(this.txtListName.Text,
                                "List automatically generated with a tool from a definition file.", 100);
                this.SaveLogAndNotifyUser(nodo, "ListCreation");
            }
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            //Web service call for update the list definition.
            SPSReference.Lists service;
            using (service = new Lists())
            {
                service.Url = System.Configuration.ConfigurationManager.AppSettings["serviceUrl"];
                service.UseDefaultCredentials = false;
                service.Timeout = System.Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["serviceTimeout"]);
                NetworkCredential credential = new NetworkCredential(
                    System.Configuration.ConfigurationManager.AppSettings["serviceUserName"],
                    System.Configuration.ConfigurationManager.AppSettings["servicePassword"],
                    System.Configuration.ConfigurationManager.AppSettings["serviceDomain"]);
                service.Credentials = credential;

                //Xml objects required for the update operation.
                XmlNode ndList = service.GetList(this.txtListName.Text);
                this.txtGUID.Text = ndList.SelectSingleNode("/").Attributes["ID"].Value.Replace("{", "").Replace("}", "");
                XmlNode ndVersion = ndList.Attributes["Version"];

                XmlDocument xmlDoc = new System.Xml.XmlDocument();

                XmlNode ndAddedFields = xmlDoc.CreateNode(XmlNodeType.Element,
                    "Fields", "");
                ndAddedFields.InnerXml = GetXmlForUpdateList();
                var nodo = service.UpdateList(this.txtGUID.Text, null, ndAddedFields, null, null, ndVersion.Value);
                this.SaveLogAndNotifyUser(nodo, "ListUpdate");
            }
        }

        private void SaveLogAndNotifyUser(XmlNode nodo, String operationType)
        {
            String userMessage = "";
            if (operationType == "ListUpdate")
                userMessage =
                    "Your changes were applied successfully, an operation result log was generated and stored at the following location:\n\r";
            else
                userMessage =
                    "Your new list was generated successfully, an operation result log was generated and stored at the following location:\n\r";

            if (operationType != "ListUpdate")
            {
                String listID = nodo.SelectSingleNode("/").Attributes["ID"].Value.ToString();
                this.txtGUID.Text = listID.Replace("{", "").Replace("}", "");
            }
            String strRutaLog = Application.StartupPath + "\\" + operationType + "_" +
                                DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + this.txtGUID.Text + ".log";
            System.IO.File.WriteAllText(strRutaLog, nodo.OuterXml);
            System.Windows.Forms.MessageBox.Show(
                 userMessage +
                strRutaLog);
        }

        private String GetXmlForUpdateList()
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < this.dgvIdentifiedFieldsList.Rows.Count; i++)
            {
                sb.Append(String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.UpdateListMethodWraper,
                    FieldSchemaGenerationHelper.GetFieldDefinition(this.dgvIdentifiedFieldsList.Rows[i].Cells[0].Value.ToString(), this.dgvIdentifiedFieldsList.Rows[i].Cells[2].Value.ToString()), (i + 1).ToString()));
            }
            return sb.ToString();
        }
    }
}
